package com.jss.lanouhn.fristapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.activity.HomeAuthorActivity;
import com.jss.lanouhn.fristapp.activity.HomeItemDetail;
import com.jss.lanouhn.fristapp.activity.HomeTitleActivity;
import com.jss.lanouhn.fristapp.modles.BaseSecondUse;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by lanouhn on 16/8/27.
 */
public class BaseSecondAdapter extends RecyclerView.Adapter<BaseSecondAdapter.HomeViewHolder>{
    private BaseSecondUse infos;
    private Context context;

    public BaseSecondAdapter(BaseSecondUse infos, Context context) {
        this.infos = infos;
        this.context = context;
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_home_big,null);
        HomeViewHolder holder=new HomeViewHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(final HomeViewHolder holder, int position) {
        final BaseSecondUse.DataBean.PostsBean bean=infos.getData().getPosts().get(position);
        holder.tv.setText(bean.getColumn().getCategory());
        holder.tv_up_right.setText(bean.getColumn().getTitle());
        holder.tv_up_left.setText(bean.getAuthor().getNickname());
        Picasso.with(context).load(bean.getAuthor().getAvatar_url()).into(holder.iv_small);
        Picasso.with(context).load(bean.getCover_image_url()).into(holder.iv_big);
        holder.tv_down_right.setText(bean.getTitle());
        holder.tv_down_left.setText(bean.getLikes_count()+"");

        holder.tv_up_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomeTitleActivity.class);
                intent.putExtra("id", bean.getColumn().getId() + "");
                context.startActivity(intent);
            }
        });

        holder.tv_up_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomeAuthorActivity.class);
                intent.putExtra("id", "" + bean.getAuthor().getId());
                context.startActivity(intent);
            }
        });

        holder.tv_down_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomeItemDetail.class);
                intent.putExtra("id", "" + bean.getId());
                context.startActivity(intent);
            }
        });
        holder.iv_big.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomeItemDetail.class);
                intent.putExtra("id", "" + bean.getId());
                context.startActivity(intent);
            }
        });

        holder.iv_like.setOnClickListener(new View.OnClickListener() {
            boolean change = true;

            @Override
            public void onClick(View v) {
                if (change) {
                    holder.iv_like.setBackgroundResource(R.mipmap.ic_action_compact_favourite_selected);
                    holder.tv_down_left.setText((bean.getLikes_count() + 1) + "");
                    change = false;
                } else {
                    holder.iv_like.setBackgroundResource(R.mipmap.ic_action_compact_favourite_normal);
                    holder.tv_down_left.setText(bean.getLikes_count() + "");
                    change = true;
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return infos.getData().getPosts().size();
    }

    public class HomeViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_up_right, tv_up_left, tv_down_right, tv_down_left,tv;
        private ImageView iv_big,iv_like;
        private CircleImageView iv_small;

        public HomeViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.btn_home_item_big);
            tv_up_right = (TextView) itemView.findViewById(R.id.tv1_home_item_big);
            tv_up_left = (TextView) itemView.findViewById(R.id.tv2_home_item_big);
            tv_down_right = (TextView) itemView.findViewById(R.id.tv3_home_item_big);
            tv_down_left = (TextView) itemView.findViewById(R.id.tv4_home_item_big);
            iv_big = (ImageView) itemView.findViewById(R.id.iv_home_item_big);
            iv_small = (CircleImageView) itemView.findViewById(R.id.icon_home_item_big);
            iv_like = (ImageView) itemView.findViewById(R.id.iv2_home_item_big);
        }
    }
}
